package com.ryskoo.controller.product;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ryskoo.catering.productcate.domain.ProductCategoryPO;
import com.ryskoo.catering.productcate.service.IProductCategoryService;
import com.ryskoo.customer.vendor.service.IVendorService;
import com.ryskoo.framework.core.bean.Pager;
import com.ryskoo.framework.core.bean.QueryMessageDTO;
import com.ryskoo.framework.core.bean.ReturnCodeEnum;
import com.ryskoo.framework.core.bean.ReturnMessageDTO;
import com.ryskoo.framework.core.controller.BasicHandler;
import com.ryskoo.framework.core.utils.LoginUtil;

@Controller
@RequestMapping("/vendor/cater/productcate")
public class ProductCateController extends BasicHandler<ProductCategoryPO> {
    private static final String TEMPLATE_PATH = "system/productcate";

    private static final String MODULE_NAME = "ProductCate";
    /**
     * 基类构造方法.
     */
    public ProductCateController() {
        super(TEMPLATE_PATH, MODULE_NAME);
    }

    private IProductCategoryService productCategoryService;

    @Autowired
    private IVendorService vendorService;

    /**
     * 消费者注入
     *
     * @param productCateConsumer 消费者基类
     */
    @Autowired
    protected void setService(IProductCategoryService productCategoryService) {
        this.productCategoryService = productCategoryService;
        super.setService(productCategoryService);
    }
    
    
    @Override
	protected void buildListPageData(Model model, HttpServletRequest request) {
    	String vendorId = LoginUtil.getLoginVendorUuid();
		model.addAttribute("vendorId", vendorId);
	}




	@Override
	protected void buildAddPageData(Model model, HttpServletRequest request) {
		String vendorId = LoginUtil.getLoginVendorUuid();
		model.addAttribute("vendorId", vendorId);
	}




	@Override
	protected void buildEditPageData(Model model, ProductCategoryPO po, HttpServletRequest request) {
		String vendorId = LoginUtil.getLoginVendorUuid();
		model.addAttribute("vendorId", vendorId);
	}




	/**
     * 添加菜品分类
     * @param dto
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public ReturnMessageDTO<ProductCategoryPO> add(@RequestBody ProductCategoryPO po) {
        ReturnMessageDTO<ProductCategoryPO> returnMessage = new ReturnMessageDTO<>();
        try {
            // 查找最大的position
            int maxPosition = productCategoryService.getMaxPosition(po.getVendorUuid());
            po.setPosition(maxPosition+1);
            productCategoryService.create(po);
            returnMessage.setReturnCode(ReturnCodeEnum.SUCCESS.getReturnCode());
            returnMessage.setReturnNote("新增成功");
            returnMessage.setReturnMessage(po);
        } catch (Exception e) {
            returnMessage.setReturnCode(ReturnCodeEnum.ERROR.getReturnCode());
            returnMessage.setReturnNote("新增失败");
        }
        return returnMessage;
    }


    @PostMapping({"/stop"})
    @ResponseBody
    public ReturnMessageDTO stop(@RequestBody List<String> uuids){
        ReturnMessageDTO<List<ProductCategoryPO>> returnMessageDTO = new ReturnMessageDTO<>();
        try {
            productCategoryService.changeStatus(uuids,false);
            returnMessageDTO.setReturnCode(ReturnCodeEnum.SUCCESS.getReturnCode());
            returnMessageDTO.setReturnNote("停用成功");
        }catch (Exception e){
            returnMessageDTO.setReturnCode(ReturnCodeEnum.FAIL.getReturnCode());
            returnMessageDTO.setReturnNote(e.getMessage());
        }
        return returnMessageDTO;
    }

    @PostMapping({"/start"})
    @ResponseBody
    public ReturnMessageDTO start(@RequestBody List<String> uuids){
        ReturnMessageDTO<List<ProductCategoryPO>> returnMessageDTO = new ReturnMessageDTO<>();
        try {
            productCategoryService.changeStatus(uuids,true);
            returnMessageDTO.setReturnCode(ReturnCodeEnum.SUCCESS.getReturnCode());
            returnMessageDTO.setReturnNote("启用成功");
        }catch (Exception e){
            returnMessageDTO.setReturnCode(ReturnCodeEnum.FAIL.getReturnCode());
            returnMessageDTO.setReturnNote(e.getMessage());
        }
        return returnMessageDTO;
    }


    @PostMapping({"/up"})
    @ResponseBody
    public ReturnMessageDTO up(@RequestBody String uuid){
        ReturnMessageDTO<List<ProductCategoryPO>> returnMessageDTO = new ReturnMessageDTO<>();
        try {
            productCategoryService.up(uuid);
            returnMessageDTO.setReturnCode(ReturnCodeEnum.SUCCESS.getReturnCode());
            returnMessageDTO.setReturnNote("上移成功");
        }catch (Exception e){
            returnMessageDTO.setReturnCode(ReturnCodeEnum.FAIL.getReturnCode());
            returnMessageDTO.setReturnNote(e.getMessage());
        }
        return returnMessageDTO;
    }

    @PostMapping({"/down"})
    @ResponseBody
    public ReturnMessageDTO down(@RequestBody String uuid){
        ReturnMessageDTO<List<ProductCategoryPO>> returnMessageDTO = new ReturnMessageDTO<>();
        try {
            productCategoryService.down(uuid);
            returnMessageDTO.setReturnCode(ReturnCodeEnum.SUCCESS.getReturnCode());
            returnMessageDTO.setReturnNote("下移成功");
        }catch (Exception e){
            returnMessageDTO.setReturnCode(ReturnCodeEnum.FAIL.getReturnCode());
            returnMessageDTO.setReturnNote(e.getMessage());
        }
        return returnMessageDTO;
    }

}
